package com.pls.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.pls.actionObjects.GetEmployeeDetailsRequest;
import com.pls.actionObjects.GetEmployeeDetailsResponse;
import com.pls.actionObjects.SearchRequest;
import com.pls.actionObjects.SearchResponse;
import com.pls.constants.ValidationErrorEnum;
import com.pls.dao.DAOFactory;
import com.pls.dao.EmployeeDAO;
import com.pls.domain.Employee;
import com.pls.domain.EmployeeWorkstation;
import com.pls.domain.ExceptionError;
import com.pls.domain.ValidationError;

public class GetEmployeeDetailsHandler extends BaseRequestHandler<GetEmployeeDetailsResponse, GetEmployeeDetailsRequest>{

	public GetEmployeeDetailsHandler(Class<GetEmployeeDetailsResponse> tResponse) {
		super(tResponse);
		// TODO Auto-generated constructor stub
	}
	
	protected ArrayList<ValidationError> validate(GetEmployeeDetailsRequest request) {
		logger.debug("- validate method entered");
		ArrayList<ValidationError> errors = new ArrayList<ValidationError>();
		ValidationError valError;

		// Check if request is null
		if (request == null) {
			valError = new ValidationError();
			valError.setMessage("Request is null.");
			valError.setErrorName(ValidationErrorEnum.NullRequest);
			errors.add(valError);
			logger.debug(this.getClass() + "- null request. Exiting validate method.");
			return errors;
		}
		
		 //check if employee object is null
		if (request.getEmployee() == null) {
			valError = new ValidationError();
			valError.setMessage("Request cannot be null.");
			valError.setErrorName(ValidationErrorEnum.InvalidRequestParameter);
			errors.add(valError);
		}
		
		 //check if employee first name is null
		if (request.getEmployee().getFirstName() == null) {
			valError = new ValidationError();
			valError.setMessage("First name cannot be null.");
			valError.setErrorName(ValidationErrorEnum.InvalidRequestParameter);
			errors.add(valError);
		}
		
		 //check if employee first name is null
		if (request.getEmployee().getLastName() == null) {
			valError = new ValidationError();
			valError.setMessage("Last name cannot be null.");
			valError.setErrorName(ValidationErrorEnum.InvalidRequestParameter);
			errors.add(valError);
		}

		logger.debug("- validate method exited.");
		return errors;
	}

	protected GetEmployeeDetailsResponse process(GetEmployeeDetailsRequest request) {

		GetEmployeeDetailsResponse response = new GetEmployeeDetailsResponse();
		// call data layer
		EmployeeDAO empDao = DAOFactory.getEmployeeDAO();
		EmployeeWorkstation employeeDetails = new EmployeeWorkstation();

		try {
			logger.debug("- process method entered.");
			

			//TODO Get wildcard for projects	
			employeeDetails = empDao.getEmployeeDetailsByName(request.getEmployee().getFirstName(), request.getEmployee().getLastName());
			
			//set the response
			response.setEmployeeDetails(employeeDetails);

		} catch (SQLException e) {
			ExceptionError sqlError = new ExceptionError(e.getClass()
					.toString(),
					"Sorry, an SQL error occurred while accessing the DB.");
			response.setExceptionError(sqlError);
			logger.debug(this.getClass() + "- sql exception:" + e.getMessage());
		} catch (Exception e) {
			ExceptionError sqlError = new ExceptionError(e.getClass()
					.toString(),
					"Sorry, an error occurred while accessing the DB.");
			response.setExceptionError(sqlError);
			logger.debug(this.getClass() + "- exception:" + e.getMessage());
		}
		
		logger.debug(this.getClass() + "- process method exited.");
		return response;
	}

}
